package com.example.forum.services.impl;

import com.example.forum.entity.Article;
import com.example.forum.entity.ArticleReply;
import com.example.forum.entity.User;
import com.example.forum.exception.CustomException;
import com.example.forum.mapper.ArticleMapper;
import com.example.forum.mapper.ArticleReplyMapper;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.utils.StringUtil;
import com.example.forum.vo.ArticleReplyVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.example.forum.constant.MessageConstant.*;

@Service
@Slf4j
public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Autowired
    private ArticleReplyMapper articleReplyMapper;

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 创建回复
     *
     * @param id
     * @param content
     * @param user
     */
    @Transactional
    public void create(Long id, String content, User user) throws CustomException {
        // 参数校验
        if (id == null || StringUtil.isEmpty(content)) {
            throw new CustomException(PARAMETER_VERIFICATION_FAILED);
        }

        // 文章信息校验
        Article article = articleMapper.getByIdArticle(id);
        if (article == null) {
            throw new CustomException(ARTICLE_NOT_EXISTS);
        }
        if (article.getDeleteState() == 1) {
            throw new CustomException(ARTICLE_DELETE_FAILED);
        }
        // 更新文章回复数
        Integer row = articleMapper.updateReplyCount(id);

        if (row != 1) {
            throw new CustomException(SERVER_ERROR);
        }

        // 封装默认值
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(id);
        articleReply.setContent(content);
        articleReply.setDeleteState(0);
        articleReply.setLikeCount(0);
        articleReply.setState(0);
        articleReply.setPostUserId(user.getId());
        // TODO
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);

        // 设置当前时间
        Date now = new Date();
        articleReply.setCreateTime(now);
        articleReply.setUpdateTime(now);
        log.info("创建回复: {}", articleReply);
        articleReplyMapper.create(articleReply);
    }

    /**
     * 获取回复列表
     *
     * @param articleId
     * @return
     */
    public List<ArticleReplyVO> getReplyList(Long articleId) throws CustomException {
        // 参数校验
        if (articleId == null) {
            throw new CustomException(PARAMETER_VERIFICATION_FAILED);
        }

        // 文章信息校验
        Article article = articleMapper.getByIdArticle(articleId);
        if (article == null || article.getDeleteState() == 1) {
            throw new CustomException(ARTICLE_NOT_EXISTS);
        }

        // 获取回复列表
        return articleReplyMapper.getReplyList(articleId);
    }
}
